package com.broada.apm.model.performance;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.broada.apm.model.performance.IndicatorGroups.Apdex;
import com.broada.apm.model.performance.IndicatorGroups.ResponseTime;
import com.broada.apm.model.performance.IndicatorGroups.Throughput;
import com.broada.apm.model.performance.IndicatorGroups.UserGroup;

public abstract class BasePerformance extends AbstractPerformance {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final String _STATUS = "_status";
    private static final String STATUS = "status";
    private static final String BUSINESS_STATUS = "businessStatus";
    private static final String PROTOCOL = "protocol";
    private static final String KEY = "key";
    private static final String RESOURCE_ID = "resourceId";
    private static final String SERVICE_ID = "serviceId";
    private static final String DOMAIN_ID = "domainId";
    private static final String APPLICATION_ID = "applicationId";
    private static final String DIMENSION_ID = "dimensionId";
    private static final String DIMENSION_TYPE = "dimensionType";
    private static final String TIMESTAMP = "timestamp";
    private static final String ID = "id";
    private static final String SUBPROTOCOL = "subProtocol";
    private static final String TRANSACTIONCHANNELNAME = "transactionChannelName";
    private static final String TRANSACTIONTYPENAME = "transactionTypeName";
    
    public final static Throughput throughput = new Throughput();
    public final static Apdex apdex = new Apdex();
    public final static ResponseTime responseTime = new ResponseTime();
    public final static UserGroup user = new UserGroup();
    
    public final static Throughput busthroughput = new Throughput("business");
    public final static Apdex busapdex = new Apdex("business");
    public final static ResponseTime busresponseTime = new ResponseTime("business");
    public final static UserGroup bususer = new UserGroup("business");
    
    public String getId() {
        return getString(ID);
    }

    public void setId(String id) {
        putString(ID, id);
    }

    public Date getTimestamp() {
        return getDate(TIMESTAMP);
    }

    public void setTimestamp(Date timestamp) {
        putDate(TIMESTAMP, timestamp);
    }

    public String getDimensionType() {
        return getString(DIMENSION_TYPE);
    }

    public void setDimensionType(String dimensionType) {
        putString(DIMENSION_TYPE, dimensionType);
    }

    public String getDimensionId() {
        return getString(DIMENSION_ID);
    }

    public void setDimensionId(String dimensionId) {
        putString(DIMENSION_ID, dimensionId);
    }

    public String getApplicationId() {
        return getString(APPLICATION_ID);
    }

    public void setApplicationId(String applicationId) {
        putString(APPLICATION_ID, applicationId);
    }

    public String getServiceId() {
        return getString(SERVICE_ID);
    }

    public void setServiceId(String serviceId) {
        putString(SERVICE_ID, serviceId);
    }

    public String getDomainId() {
        return getString(DOMAIN_ID);
    }

    public void setDomainId(String domainId) {
        putString(DOMAIN_ID, domainId);
    }
    public String getSubProtocol() {
        return getString(SUBPROTOCOL);
    }

    public void setSubProtocol(String subProtocol) {
        putString(SUBPROTOCOL, subProtocol);
    }
    public String getTransactionChannelName() {
    	return getString(TRANSACTIONCHANNELNAME);
    }
    public void setTransactionChannelName(String transactionChannelName) {
    	putString(TRANSACTIONCHANNELNAME, transactionChannelName);
    }
    public String getTransactionTypeName() {
    	return getString(TRANSACTIONTYPENAME);
    }
    public void setTransactionTypeName(String transactionTypeName) {
    	putString(TRANSACTIONTYPENAME, transactionTypeName);
    }
    public String getResourceId() {
        return getString(RESOURCE_ID);
    }

    public void setResourceId(String resourceId) {
        putString(RESOURCE_ID, resourceId);
    }
    public boolean isKey() {
        return getBool(KEY);
    }

    public void setKey(boolean key) {
        putBool(KEY, key);
    }

    public int getProtocol() {
        return getInt(PROTOCOL);
    }

    public void setProtocol(int protocol) {
        putInt(PROTOCOL, protocol);
    }

    public int getStatus() {
        return getInt(STATUS);
    }

    public void setStatus(int status) {
        putInt(STATUS, status);
    }
    
    public int getBusinessStatus() {
    	return getInt(BUSINESS_STATUS);
    }
    
    public void setBusinessStatus(int businessStatus) {
    	putInt(BUSINESS_STATUS, businessStatus);
    }

    public void setStatusOf(String key, Integer status) {
        putInt(key + _STATUS, status);
    }

    public Integer getStatusOf(String key) {
        return getInt(key + _STATUS);
    }

    public Map<String, Integer> getStatusMap() {
        Map<String, Integer> status = new HashMap<String,Integer>();
        for(String key : this.keySet()) {
            if(key.endsWith(_STATUS)) {
                status.put(key, getInt(key));
            }
        }
        return status;
    }

    public BasePerformance copy() {
        return (BasePerformance) clone();
    }
    
    public final void update(){
        throughput.update("system", this);
        apdex.update("system", this);
        responseTime.update("system", this);
        user.update("system", this);
        busthroughput.update("business", this);
        busapdex.update("business", this);
        busresponseTime.update("business", this);
        bususer.update("business", this);
        perfupdate();
    }
    protected abstract void perfupdate();
}
